#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
  printf("我变成了一个进程：%d\n", getpid());
  
  pid_t id = fork();
  if(id == 0)
  {
    //执行另一个程序
    sleep(2);
    printf("下面的代码都是子进程在执行：\n");
    char* myargv[]={
      (char*)"/cmd/othercmd",
      (char*)"-a",
      (char*)"-b",
      NULL
    };

    char* myenv[]={
      (char*)"PATH=/home/lbw/linux/lesson17/exec/cmd",
      NULL
    };
    extern char **environ;
    putenv((char*)"lbw=nb"); 
     execvpe("./cmd/othercmd", myargv, environ);
    //execvpe("./cmd/othercmd", myargv, myenv); //覆盖式的使用全新的环境变量
    //execvpe("./cmd/othercmd", myargv, environ); //使用父进程的环境变量表

    exit(3);







    //execl("/usr/bin/bash", "bash", "./test.sh", NULL);
    //char * argv[]={
    //   (char*) "ls",
    //   (char*) "-a",
    //   (char*)"-l",
    //   (char*)"--color=auto",
    //   NULL
    //};
   
    //execl("./othercmd", "othercmd", NULL);
    //execvp("ls", argv);
    //execv("/usr/bin/ls", argv);
    //execl("/usr/bin/ls", "-a", "-l", NULL);//程序替换
    //execlp("ls","ls", "-a", "-l", NULL);//程序替换

    // execl("/usr/bin/top","top", "-d", "1", "-n", "3", NULL);
    //走到这里说明替换失败
    //exit(0);
  }
  int status = 0;
  pid_t rid = waitpid(id, &status, 0);
  if(rid > 0)
  {
     printf("wait success, exit_code:%d\n", WEXITSTATUS(status));
  }
}
